/******************************************************************************
 * Copyright (C) Ultraleap, Inc. 2011-2020.                                   *
 *                                                                            *
 * Use subject to the terms of the Apache License 2.0 available at            *
 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement           *
 * between Ultraleap and you, your company or other organization.             *
 ******************************************************************************/

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Leap.Unity {

  public static class GuiRectUtil {

    public static Vector3 Corner00(this Rect rect) {
      return new Vector3(rect.x, rect.y);
    }

    public static Vector3 Corner10(this Rect rect) {
      return new Vector3(rect.x + rect.width, rect.y);
    }

    public static Vector3 Corner01(this Rect rect) {
      return new Vector3(rect.x, rect.y + rect.height);
    }

    public static Vector3 Corner11(this Rect rect) {
      return new Vector3(rect.x + rect.width, rect.y + rect.height);
    }

    public static Rect Encapsulate(this Rect rect, Vector2 point) {
      if (point.x < rect.x) {
        rect.width += rect.x - point.x;
        rect.x = point.x;
      } else if (point.x > rect.x + rect.width) {
        rect.width = point.x - rect.x;
      }

      if (point.y < rect.y) {
        rect.height += rect.y - point.y;
        rect.y = point.y;
      } else if (point.y > rect.y + rect.height) {
        rect.height = point.y - rect.y;
      }

      return rect;
    }

    public static void SplitHorizontally(this Rect rect, out Rect left, out Rect right) {
      left = rect;
      left.width /= 2;
      right = left;
      right.x += right.width;
    }

    public static void SplitHorizontallyWithRight(this Rect rect, out Rect left, out Rect right, float rightWidth) {
      left = rect;
      left.width -= rightWidth;
      right = left;
      right.x += right.width;
      right.width = rightWidth;
    }

    public static Rect NextLine(this Rect rect) {
      rect.y += rect.height;
      return rect;
    }

    public static Rect FromRight(this Rect rect, float width) {
      rect.x = rect.width - width;
      rect.width = width;
      return rect;
    }

#if UNITY_EDITOR
    public static Rect SingleLine(this Rect rect) {
      rect.height = EditorGUIUtility.singleLineHeight;
      return rect;
    }

    public static Rect Indent(this Rect rect) {
      rect.x += EditorGUIUtility.singleLineHeight;
      rect.width -= EditorGUIUtility.singleLineHeight;
      return rect;
    }
#endif
  }
}
